<?php

namespace App\Helper;

use App\Models\V1\SmsCodeRecord;
use App\Models\V1\Box\Sms;
use Illuminate\Support\Facades\Cache;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use Ramsey\Uuid\Uuid;

/**
 * 阿里云sdk
 */
class AliyunSDK
{

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient()
    {
        $config = new Config([
            // 您的 AccessKey ID
            "accessKeyId" => config('aliyun.all.accessKeyId'),
            // 您的 AccessKey Secret
            "accessKeySecret" => config('aliyun.all.accessKeySecret')
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * 发送短信验证码
     * @param $mobile
     * @param $code
     * @return string
     */
    public function sendSms($mobile = "",  $scene = 'register')
    {
        $sign = "重庆比奇互娱网络科技";
        $template = "SMS_463200946";
        $client = self::createClient();

        try {
            $TemplateParam['code'] = rand(100000, 999999);
            $sendSmsRequest = new SendSmsRequest([
                "phoneNumbers" => $mobile,
                "signName" => $sign,
                "templateCode" => $template,
                "templateParam" => json_encode($TemplateParam)
            ]);
            $runtime = new RuntimeOptions([]);

            // 复制代码运行请自行打印 API 的返回值
            $response = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
            if ($response->body->code == 'OK') {
                // 作废本号码之前所有未使用验证码
                SmsCodeRecord::query()->where('mobile', $mobile)->update([
                    'state' => 1,
                ]);
                //添加短信发送记录
                $smsCodeLog = SmsCodeRecord::query()->create([
                    'mobile' => $mobile,
                    'scene' => $scene,
                    'code' => $TemplateParam['code'],
                    'state' => 0,
                ]);
                return ['state' => true, 'data' => $smsCodeLog];

            } else {
                return ['state' => false];
            }
        }catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            return ['state' => false, 'data' => $error->message];
        }
    }





    /**
     * 验证短信验证码
     * @param $mobile
     * @param $code
     * @return bool
     */
    public function verify_sms($mobile = "", $code = "", $client_id = "")
    {
        if (empty($mobile) || empty($code) || empty($client_id)) {
            return false;
        }
        $sms_model = new Sms();
        $sms = $sms_model->where('mobile', $mobile)->where('client_id', $client_id)->first();
        if ($sms) {
            if ($sms->code == (int)$code) {
                return true;
            }
        }
        return false;
    }


}
